package com.his.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.entity.dto.Disease;
import com.his.entity.dto.MedicalRecord;
import com.his.entity.po.DiseasePO;
import com.his.mapper.DiseaseMapper;
import com.his.mapper.MedicalMapper;
import com.his.service.IMedicalService;
import com.his.entity.vo.DiseaseVO;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class MedicalServiceImpl extends ServiceImpl<MedicalMapper, MedicalRecord> implements IMedicalService {
    @Autowired
    private MedicalMapper medicalMapper;
    @Autowired
    private DiseaseMapper diseaseMapper;

    /**
     * 创建挂号记录
     * @param medicalRecord
     * @return
     */
    @Override
    public void create(MedicalRecord medicalRecord) {
        MedicalRecord record = medicalMapper.selectOne(Wrappers.lambdaQuery(MedicalRecord.class)
                .eq(MedicalRecord::getRegisterId, medicalRecord.getRegisterId()));
        super.saveOrUpdate(record);
    }

    /**
     * 诊断
     * @param registerId
     * @return
     */
    @Override
    public List<DiseaseVO> diagnosis(Long registerId) {
        MedicalRecord medicalRecord = medicalMapper.selectOne(Wrappers.lambdaQuery(MedicalRecord.class)
                .eq(MedicalRecord::getRegisterId, registerId));
        if (Objects.isNull(medicalRecord)) {
            return List.of();
        }
        List<DiseaseVO> diseaseVOS = diseaseMapper.selectDiseaseByRegisterId(medicalRecord);
        return diseaseVOS;
    }

        /**
     * 添加疾病信息
     * <p>该方法用于向医疗记录中添加疾病信息，包括创建医疗记录（如果不存在）、
     * 插入疾病数据以及建立医疗记录与疾病之间的关联关系。</p>
     * @param diseasePO 疾病信息传输对象，包含疾病的基本信息和关联的挂号ID
     */
    @Override
    @Transactional
    public void AddDisease(DiseasePO diseasePO) {
        // 查询指定挂号ID对应的医疗记录
        MedicalRecord medicalRecord = medicalMapper.selectOne(Wrappers.lambdaQuery(MedicalRecord.class)
                .eq(MedicalRecord::getRegisterId, diseasePO.getRegisterId()));

        // 如果医疗记录不存在，则创建新的医疗记录
        if (Objects.isNull(medicalRecord)) {
            medicalRecord = new MedicalRecord();
            medicalRecord.setRegisterId(diseasePO.getRegisterId());
            medicalMapper.insert(medicalRecord);
        }

        // 将疾病PO对象转换为疾病实体对象并插入数据库
        Disease disease = BeanUtil.toBean(diseasePO, Disease.class);
        diseaseMapper.insert(disease);

        // 建立医疗记录与疾病之间的关联关系
        medicalMapper.insertMiddelTable(medicalRecord.getId(), disease.getId(), diseasePO.getAttackTime());
    }


    /**
     * 删除疾病
     * @param diseaseId
     */
    @Override
    @Transactional
    public void deleteDisease(Long diseaseId) {
        diseaseMapper.deleteById(diseaseId);
        diseaseMapper.deleteMiddle(diseaseId);

    }
}
